﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Exam_Management.FAC
{
    /// <summary>
    /// 工厂类 
    ///  一组：杨帆 2018/5/18
    /// </summary>
    public class Factory
    {
        /// <summary>
        /// 反射创建DAL
        /// 一组：杨帆 2018/5/18
        /// </summary>
        /// <typeparam name="T">要创建实例的类的接口</typeparam>
        /// <returns></returns>
        public static T CreatDal<T>()
        {
            string str = typeof(T).Name; //获取类型名称
            Assembly assembly = Assembly.Load("Exam_Management.DAL"); //加实现类载程序集
            Type[] types = assembly.GetTypes(); //获取所有类型
            foreach (var t in types)
            {
                if (t.GetInterface(str) != null) //如果存在实现类继承了接口T，则返回实现T类，否则返回 default(T)
                {
                    var dal = (T)Activator.CreateInstance(t);
                    return dal;
                }
            }
            return default(T);
        }
    }
}
